package com.ts.content.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ts.common.core.utils.Result;
import com.ts.common.security.dto.UserLoginDto;
import com.ts.content.dto.TopicDTO;
import com.ts.content.entity.TopicEntity;
import com.ts.content.entity.TopicUserEntity;
import com.ts.content.service.TopicService;
import com.ts.content.service.TopicUserService;
import com.ts.content.vo.Topic2Vo;
import com.ts.content.vo.TopicVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 圈子
 *
 * @author admin admin@qq.com
 * @since 1.0.0 2023-02-22
 */
@RestController
@RequestMapping("/topic")
@Api(tags="圈子")
public class TopicController {
    @Autowired
    private TopicService topicService;

    @Autowired
    private TopicUserService topicUserService;

    /**以下是用于远程调用*/
    @PostMapping("/list")
    public Page<TopicEntity> list(@RequestBody Page<TopicEntity> page){
        return topicService.page(page);
    }

    @PostMapping("/save")
    public Result<TopicEntity> save(@RequestBody TopicEntity topicEntity){
        topicService.save(topicEntity);
        return new Result<>();
    }
    @PostMapping("/page")
    public IPage<TopicDTO> page(@RequestBody Page<TopicDTO> page){
        return topicService.getTopicList(page);
    }
    /**以上用于远程调用*/

    //添加圈子
    @PostMapping("/topicSave")
    public Result topicSave(@RequestBody TopicEntity topicEntity,UserLoginDto userLoginDto){
        topicEntity.setCreateTime(new Date());
        topicEntity.setUserId(userLoginDto.getUserId());
        boolean save = topicService.save(topicEntity);
        return new Result().Viewok(topicEntity.getId());
    }

    //查询当前用户加入的圈子
    @GetMapping("/currentUserJoinTopic")
    public Result currentUserJoinTopic(Long page, UserLoginDto userLoginDto){
        //获取当前登录的id
        Integer userId = userLoginDto.getUserId();

        Page page1 = new Page();
        page1.setCurrent(page);

        //查询当前用户加入的圈子；
        IPage userTopicList = topicService.getUserTopicList(page1, userId);

        return new Result().Viewok(userTopicList);
    }

    //获取帖子列表
    @GetMapping("/topicList")
    public Result topicList(Integer classId, Integer page){

        QueryWrapper<TopicEntity> queryWrapper = new QueryWrapper();

        Page<TopicEntity> topicPage = new Page();
        topicPage.setCurrent(page);

        //根据类目id查询帖子的信息列表
        List<TopicVo> topicVoPage = topicService.topicVoListByClassId(classId,topicPage);

        return new Result().Viewok(topicVoPage);
    }

    //根据圈子id查询数据
    @GetMapping("/detail")
    public Result detail(@RequestParam("id") Integer id){
        Topic2Vo topic2Vo = topicService.getUserTopicById(id);
        return new Result().Viewok(topic2Vo);
    }

    //当前用户加入圈子
    @PostMapping("/joinTopic")
    public Result joinTopic(@RequestBody TopicUserEntity topicUserEntity,UserLoginDto userLoginDto){
        topicUserEntity.setUserId(userLoginDto.getUserId());
        topicUserEntity.setCreateTime(new Date());

        topicUserService.save(topicUserEntity);
        return new Result();
    }
}